#include<func.h>
int main(int argc,char*argv[])
{
    // ./udp_server 192.168.213.128  
    ARGS_CHECK(argc,3);
    int sockfd =socket(AF_INET,SOCK_DGRAM,0); //IPV4,udp
    //为了bind
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    //客户端不用bind
    sendto(sockfd,"zaima?\n",8,0,(struct sockaddr*)&addr,sizeof(addr));
    //需要客户端先发，告诉服务端位置
    fd_set rdset;
    char buf[4096] = {0};
    //准备好客户端的地址
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    while(1)
    {
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        bzero(buf,sizeof(buf));
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(sockfd,&rdset))
        {
            //客户端来消息
            ssize_t sret = recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client_addr,&client_addr_len);
            if(sret == 0)
            {
                //代表客户端退出了
                break;
            }
            printf("buf =%s\n",buf);
        }
        if(FD_ISSET(STDIN_FILENO,&rdset))
        {
            //自己写消息
            ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
            if(sret == 0)
            {
                //给客户端发一个空包表示断开连接
                sendto(sockfd,buf,0,0,(struct sockaddr*)&client_addr,client_addr_len);
                break;
            }
            sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client_addr,client_addr_len);
        }
    }
    close(sockfd);
    return 0;        
}
